<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Comment extends CI_Model {
    var $table_name = "comments";

    function addComment($text, $id, $name, $user_id) {
  	  $data = array('user_name'=>$name, 'deal_id'=>$id, 'text'=>$text, 'user_id'=>$user_id
    		 );
    	$this->db->insert($this->table_name, $data);
    }

    function answerComment($id, $text) {
      $this->db->where('id',$id);
      $this->db->set('answer',$text);
      $this->db->update($this->table_name);
    }
    
  function getCommentsByDealId($id) {
   	$this->db->where('deal_id', $id);
   	$data = $this->db->get($this->table_name);
     return $data->result();
   }

  function getCommentById($id) {
    $this->db->where('id', $id);
    $data = $this->db->get($this->table_name);
    return $data->result();
  }

  function getAllComments() {
    $data = $this->db->get($this->table_name);
    return $data->result();
  }

	function belongsToMe(){

	}   
	
	function pageCount(){
		$query = $this->db->get($this->table_name);
		$rowcount = $query->num_rows();
		$pagecount = 0;
		if($rowcount % 20 == 0){
			$pagecount = intval($rowcount/20);
		}else{
			$pagecount = intval($rowcount/20)+1;
		}
		return $pagecount;
	} 

  function deleteComment($id){
    $this->db->where('id', $id);
    $this->db->delete($this->table_name); 
  }

  function editCategory($id, $name){
    $this->db->where('id',$id);
    $this->db->set('name',$name);
    $this->db->update($this->table_name);
  }

}